package cn.maihe.elg.operation.config;

import cn.maihe.elg.operation.common.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Description
 * @Author caixh
 * @Date 2021/4/9.
 */
@Configuration
public class ImageWebConfiguration implements WebMvcConfigurer {

    // 注入我们配置文件中写好的图片保存路径
//    @Value("${user.filepath}")
//    private String filePath;
    @Resource
    private ImageUploadConfig imageUploadConfig;
    @Resource
    private UploadConfig uploadConfig;
    @Resource
    private ElgResourceConfig elgResourceConfig;

    // 自定义资源映射
    // 访问图片示例：http://localhost:3000/api/images/图片名称.jpg
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/images/**")
                .addResourceLocations("file:"+ imageUploadConfig.getPath());
        registry.addResourceHandler("/api/regist/images/**")
                .addResourceLocations("file:"+ imageUploadConfig.getPath()+ Constants.REGIST_IMAGE_PATH);

        UploadConfig.Upload upload = uploadConfig.getUpload().get("invoice");
        if(upload==null){
            return;
        }
        registry.addResourceHandler("/apii/invoice/**")
                .addResourceLocations("file:"+ upload.getPath());

        registry.addResourceHandler("/filePath/**")
                .addResourceLocations("file:"+ elgResourceConfig.getBasePath()+"/");

    }
}
